% fname: routing_dyad_0_6__sel_bufferlevel__topology_8x8__.m
% ./noxim -routing dyad 0.6 -sel bufferlevel -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 2 -hs 6 0.2 -hs 7 0.2 -hs 14 0.2 -hs 15 0.2 

function [max_pir, max_throughput, min_delay] = routing_dyad_0_6__sel_bufferlevel__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0015        19.3441      0.0131419            101            776           6203
           0.0015        22.2008      0.0131674            136            777           6215
           0.0015        19.9212      0.0131315            100            761           6093
           0.0015        20.9667      0.0135229            145            812           6491
           0.0015        19.5896      0.0127433             74            714           5709
           0.0015        21.9655      0.0135065            158            783           6267
           0.0015        20.3207      0.0122729            108            736           5891
           0.0015        23.6582       0.013375            173            749           5992
           0.0015        21.8238       0.013964            143            823           6591
           0.0015        21.9842      0.0130733            177            759           6066
           0.0015         22.127      0.0137585            134            811           6494
           0.0015         20.607      0.0124625            110            748           5982
           0.0015        21.2297      0.0131229            125            775           6194
           0.0015         23.125      0.0131107            208            800           6398
           0.0015        20.0531      0.0125479             86            753           6023
           0.0015        21.1641      0.0137039            153            780           6249
           0.0015        20.6425       0.012175            120            730           5844
           0.0015        19.3769      0.0122306            135            711           5675
           0.0015        20.2011      0.0128728            121            721           5767
           0.0015        21.4018      0.0129406            103            789           6315
           0.0017        23.8452      0.0154892            231            898           7187
           0.0017        22.4551      0.0144671            149            824           6597
           0.0017        23.3329      0.0145975            197            862           6890
           0.0017        23.5603       0.014543            192            887           7097
           0.0017        21.2189      0.0140121            110            868           6950
           0.0017        21.1422      0.0141896            126            851           6811
           0.0017        23.2674       0.013875            156            860           6882
           0.0017        21.2916      0.0134877            139            823           6582
           0.0017        22.2347      0.0152225            163            899           7185
           0.0017        24.6395      0.0146926            167            896           7170
           0.0017        20.6623      0.0133155            189            838           6711
           0.0017        22.7821      0.0133226            157            826           6608
           0.0017        21.8767      0.0151118            101            860           6891
           0.0017        22.2821      0.0146354            125            879           7025
           0.0017        22.5878      0.0139877            110            854           6826
           0.0017         22.961       0.014339            165            846           6768
           0.0017        22.4061      0.0142722            157            884           7079
           0.0017        20.1827      0.0134028             80            843           6755
           0.0017        22.8188      0.0151674            185            894           7159
           0.0017        23.3922      0.0141411            220            877           7014
           0.0019        24.7319      0.0172105            113            981           7848
           0.0019        23.9234      0.0156291            138            953           7627
           0.0019        28.5571      0.0165202            226           1025           8194
           0.0019         26.269      0.0168217            264           1026           8209
           0.0019        22.5964      0.0154611            113            944           7545
           0.0019        22.1544      0.0149355            103            926           7408
           0.0019        27.8216      0.0170225            238           1037           8307
           0.0019        24.7436      0.0150081            163            932           7444
           0.0019        24.7487      0.0147857            209            931           7452
           0.0019         26.501       0.015873            169           1002           8000
           0.0019        24.0416      0.0162945            181            961           7691
           0.0019        28.5679      0.0163125            246            979           7830
           0.0019        24.8168      0.0167331            190            988           7898
           0.0019        26.9388      0.0155456            198            980           7835
           0.0019        25.5662      0.0158427            201            982           7858
           0.0019        28.0378      0.0155496            175            978           7837
           0.0019        26.7746      0.0162917            159            976           7820
           0.0019        24.2448      0.0158463            156            968           7733
           0.0019        23.7059      0.0154839            118            959           7680
           0.0019        26.3649      0.0156855            251            973           7780
           0.0021        25.2357      0.0163548            215           1014           8112
           0.0021        28.7765       0.016625            164           1065           8512
           0.0021        30.7519      0.0171653            258           1064           8514
           0.0021        35.3705      0.0177871            336           1139           9107
           0.0021        27.1215      0.0182738            166           1152           9210
           0.0021        28.0232      0.0170952            293           1077           8616
           0.0021        30.0674      0.0169583            304           1069           8547
           0.0021        32.3828      0.0180222            407           1118           8939
           0.0021        29.5649      0.0181947            152           1110           8879
           0.0021        24.5353       0.017373            154           1061           8478
           0.0021        27.3477      0.0172077            154           1067           8535
           0.0021        26.2844      0.0167341            197           1055           8434
           0.0021        28.6011      0.0173242            211           1108           8870
           0.0021        24.7539      0.0167319            190           1036           8299
           0.0021        30.1746      0.0170198            229           1071           8578
           0.0021        25.6276      0.0168234            125           1058           8479
           0.0021        26.9485      0.0178488            189           1106           8853
           0.0021        29.4726      0.0176448            235           1113           8893
           0.0021        27.5624      0.0170635            174           1074           8600
           0.0021        27.6165      0.0197098            230           1103           8830
           0.0023        38.4387      0.0188555            711           1208           9654
           0.0023        32.3485      0.0201578            200           1231           9837
           0.0023        33.5492      0.0184375            300           1180           9440
           0.0023        31.9753      0.0177187            278           1135           9072
           0.0023        33.1716      0.0183066            264           1171           9373
           0.0023        32.5915      0.0179316            275           1148           9181
           0.0023        32.4283      0.0193176            244           1179           9427
           0.0023        33.9448      0.0190143            249           1160           9279
           0.0023         29.808      0.0197157            220           1224           9779
           0.0023        29.5688      0.0187266            193           1199           9588
           0.0023        32.5184      0.0174336            453           1117           8926
           0.0023        33.0409      0.0189782            326           1197           9565
           0.0023        30.4754      0.0186964            259           1178           9423
           0.0023        32.8288      0.0182738            451           1151           9210
           0.0023        27.2534      0.0184821            245           1164           9315
           0.0023        32.1415      0.0182461            203           1166           9342
           0.0023        32.7176      0.0200417            317           1264          10101
           0.0023         34.658       0.018246            481           1149           9196
           0.0023        34.3533      0.0196885            338           1200           9608
           0.0023        28.7831      0.0187401            213           1180           9445
           0.0025        44.4022      0.0212828            658           1298          10386
           0.0025        41.2132      0.0209306            554           1318          10549
           0.0025        42.6941       0.020244            522           1278          10203
           0.0025        34.2263      0.0202877            278           1277          10225
           0.0025         38.697      0.0210952            436           1330          10632
           0.0025        38.6289      0.0205536            311           1296          10359
           0.0025        45.7629      0.0200313            566           1282          10256
           0.0025        36.2566      0.0199762            446           1259          10068
           0.0025        42.5329      0.0199863            659           1278          10233
           0.0025        37.6022      0.0204028            426           1287          10283
           0.0025        36.5724      0.0208281            335           1333          10664
           0.0025           37.1      0.0206391            230           1280          10237
           0.0025        30.3362      0.0196348            236           1258          10053
           0.0025         40.081      0.0202578            399           1297          10372
           0.0025        35.5674      0.0198281            291           1269          10152
           0.0025        39.4532      0.0212379            604           1315          10534
           0.0025         38.053      0.0194464            536           1226           9801
           0.0025        34.9913      0.0197402            282           1263          10107
           0.0025         31.489      0.0191348            219           1225           9797
           0.0025        42.1922      0.0203145            463           1259          10076
           0.0027        53.0245      0.0217129            801           1390          11117
           0.0027        46.5177      0.0215159            904           1356          10844
           0.0027        62.4853      0.0223809            725           1432          11459
           0.0027        46.4753      0.0218691            469           1399          11197
           0.0027        47.8857      0.0222832            424           1426          11409
           0.0027        44.6577      0.0214023            558           1370          10958
           0.0027        47.9028      0.0217207            541           1389          11121
           0.0027        50.6096      0.0225853            494           1424          11383
           0.0027        40.0366      0.0213652            342           1367          10939
           0.0027        53.2403      0.0216567            997           1365          10915
           0.0027        46.8521      0.0225957            475           1447          11569
           0.0027        42.4427      0.0206508            436           1301          10408
           0.0027        42.7443      0.0222083            338           1400          11193
           0.0027        72.8798      0.0229901           1130           1447          11587
           0.0027        48.2746      0.0217246            617           1391          11123
           0.0027        52.7876      0.0221694            837           1375          10996
           0.0027        47.2383      0.0223085            639           1385          11065
           0.0027         61.734      0.0225536            817           1421          11367
           0.0027        42.2851      0.0202656            551           1298          10376
           0.0027        49.4809      0.0219663            549           1385          11071
           0.0029        52.8338      0.0225645            527           1444          11553
           0.0029         83.286      0.0235853           1017           1486          11887
           0.0029        56.6422      0.0223008            960           1428          11418
           0.0029         75.557      0.0228848            823           1465          11717
           0.0029        114.488      0.0244375           1933           1565          12512
           0.0029        67.4892      0.0242044            946           1525          12199
           0.0029        44.8255      0.0218145            502           1398          11169
           0.0029        60.0505       0.023498            606           1504          12031
           0.0029        80.6621       0.022877           1003           1465          11713
           0.0029        59.9487      0.0238512            669           1502          12021
           0.0029        98.8243      0.0242793           1958           1554          12431
           0.0029        47.9972      0.0226191            467           1448          11581
           0.0029        88.5036      0.0239277            873           1531          12251
           0.0029        53.9314      0.0220859            728           1415          11308
           0.0029        65.4155      0.0231131            645           1456          11649
           0.0029        74.6791      0.0235098            854           1505          12037
           0.0029        52.9697      0.0230635            471           1453          11624
           0.0029        78.3957      0.0242659            922           1529          12230
           0.0029        65.7183      0.0233027            726           1491          11931
           0.0029        91.6873      0.0230098           2513           1471          11781
           0.0031         86.773      0.0248066           1037           1586          12701
           0.0031        101.281      0.0250684           1288           1605          12835
           0.0031        114.175       0.024623           1835           1552          12410
           0.0031        226.044      0.0255176           2874           1632          13065
           0.0031        130.613      0.0254023           1279           1624          13006
           0.0031        82.9196      0.0248652            771           1592          12731
           0.0031        237.181      0.0245664           3135           1572          12578
           0.0031        166.932      0.0256653           3391           1592          12730
           0.0031        92.9465      0.0248184           1044           1590          12707
           0.0031        126.454      0.0249355           1545           1596          12767
           0.0031        120.251      0.0251074           1582           1608          12855
           0.0031        126.698      0.0248175           2087           1565          12508
           0.0031        125.814      0.0251151           1589           1583          12658
           0.0031        103.681      0.0255098           2220           1634          13061
           0.0031        189.765      0.0252441           1884           1615          12925
           0.0031        95.5145      0.0242754            985           1553          12429
           0.0031          124.4       0.024748           3188           1584          12671
           0.0031        102.944      0.0249623           2344           1573          12581
           0.0031        102.465      0.0246992           1861           1582          12646
           0.0031        127.104      0.0244453           2275           1564          12516
           0.0033        196.097      0.0254941           2883           1632          13053
           0.0033         115.73       0.025209           1498           1613          12907
           0.0033        351.315      0.0261074           4644           1670          13367
           0.0033        270.667      0.0250293           3607           1602          12815
           0.0033        341.773      0.0246953           3359           1581          12644
           0.0033        272.215      0.0256445           3231           1642          13130
           0.0033        170.546       0.025502           1808           1631          13057
           0.0033        273.535       0.026379           2409           1661          13295
           0.0033        143.007      0.0257891           2260           1650          13204
           0.0033        228.164      0.0245059           2044           1568          12547
           0.0033        113.968      0.0250762           1704           1606          12839
           0.0033         110.99      0.0255527           1256           1635          13083
           0.0033         123.34      0.0255195           1362           1634          13066
           0.0033        271.611      0.0263027           2901           1683          13467
           0.0033        318.472      0.0259473           4280           1660          13285
           0.0033        244.446      0.0258145           4717           1651          13217
           0.0033        238.266      0.0259746           2414           1663          13299
           0.0033        151.753      0.0254531           2101           1630          13032
           0.0033        504.624      0.0262598           5244           1680          13445
           0.0033        283.321      0.0255605           4505           1637          13087
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
